package com.ildepinc.persistence.abstracts;

import java.util.HashMap;

import com.ildepinc.persistence.enums.NewProgressiveMethod;
import com.ildepinc.persistence.interfaces.TableGenericBehaviour;

public abstract class TableGeneric implements TableGenericBehaviour {

  private String schema;
  private String tableName;
  private HashMap<String, Object> primaryKeys;
  private Boolean pkSingleColumn;
  private Boolean pkAutoIncremental;
  private String sqlCreateTable;
  private String sqlInsert;
  private String sqlUpdate;
  private String sqlSelect;
  private String sqlDelete;
  private NewProgressiveMethod newProgressiveMethod;

  @Override
  public String composePkWhereCond() {
    String result = " WHERE ";
    HashMap<String, Object> pks = this.getPrimaryKeys();
    for (HashMap.Entry<String, Object> pk : pks.entrySet()) {
      result += pk.getKey() + " = ? AND ";
    }
    if (result.substring(result.length() - 4).equalsIgnoreCase("AND ")) {
      result = result.substring(0, (result.length() - 4));
    }
    return result;
  }

  @Override
  public NewProgressiveMethod getNewProgressiveMethod() {
    return this.newProgressiveMethod;
  }

  @Override
  public HashMap<String, Object> getPrimaryKeys() {
    return this.primaryKeys;
  }

  @Override
  public String getSchema() {
    return this.schema;
  }

  @Override
  public String getSqlCreateTable() {
    return this.sqlCreateTable;
  }

  @Override
  public String getSqlDelete() {
    String result = this.sqlDelete + this.composePkWhereCond();
    return result;
  }

  @Override
  public String getSqlInsert() {
    return sqlInsert;
  }

  @Override
  public String getSqlSelect() {
    String result = this.sqlSelect + this.composePkWhereCond();
    return result;
  }

  @Override
  public String getSqlUpdate() {
    String result = this.sqlUpdate + this.composePkWhereCond();
    return result;
  }

  @Override
  public String getTableName() {
    return this.tableName;
  }

  @Override
  public Boolean isPkAutoIncremental() {
    return this.pkAutoIncremental;
  }

  @Override
  public Boolean isPkSingleColumn() {
    return this.pkSingleColumn;
  }

  @Override
  public void setNewProgressiveMethod(NewProgressiveMethod newProgressiveMethod) {
    this.newProgressiveMethod = newProgressiveMethod;
  }

  @Override
  public void setPkAutoIncremental(Boolean pkAutoIncremental) {
    this.pkAutoIncremental = pkAutoIncremental;
  }

  @Override
  public void setPkSingleColumn(Boolean singleColumn) {
    this.pkSingleColumn = singleColumn;
  }

  @Override
  public void setPrimaryKeys(HashMap<String, Object> primaryKeys) {
    this.primaryKeys = primaryKeys;
  }

  @Override
  public void setSchema(String schema) {
    this.schema = schema;
  }

  @Override
  public void setSqlCreateTable(String sqlCreateTable) {
    this.sqlCreateTable = sqlCreateTable;
  }

  @Override
  public void setSqlDelete(String sqlDelete) {
    this.sqlDelete = sqlDelete;
  }

  @Override
  public void setSqlInsert(String sqlInsert) {
    this.sqlInsert = sqlInsert;
  }

  @Override
  public void setSqlSelect(String sqlSelect) {
    this.sqlSelect = sqlSelect;
  }

  @Override
  public void setSqlUpdate(String sqlUpdate) {
    this.sqlUpdate = sqlUpdate;
  }

  @Override
  public void setTableName(String tableName) {
    this.tableName = tableName;
  }

  @Override
  public void clear() {
    this.schema = null;
    this.tableName = null;
    this.primaryKeys = null;
    this.pkSingleColumn = null;
    this.pkAutoIncremental = null;
    this.sqlCreateTable = null;
    this.sqlInsert = null;
    this.sqlUpdate = null;
    this.sqlSelect = null;
    this.sqlDelete = null;
    this.newProgressiveMethod = null;
  }

  @Override
  public void setDefaults(String schema) {
    this.schema = schema;
  }

}
